<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="support.*, java.util.*"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Provide Degrees - Choose University</title>
</head>
<body>
	<h1>Provide Degrees</h1>
	<%
	
	student stu = (student)session.getAttribute("student");
	String firstName = (String)stu.getAttribute("firstName");
    String lastName = (String)stu.getAttribute("lastName");
	String middleName = (String)stu.getAttribute( "middleName" );
	String citizenship = (String)stu.getAttribute( "citizenship" );
	String residence = (String) stu.getAttribute( "residence" );
	String street = (String)stu.getAttribute( "street" );
	String city = (String)stu.getAttribute( "city" );
	String phone = (String)stu.getAttribute( "phone" );
	
	out.println(firstName + " " + middleName + " " + lastName +"</br>");
	out.println("Country of Citizenship: " + citizenship + "</br>");
	out.println("Country of Residence: " + residence + "</br>");
	out.println("Address </br>");
	out.println("Street: " + street + "</br>");
	out.println("City: " + city + "</br>");
	
	
	String resident = (String)stu.getAttribute( "resident" );
	if(resident != null){
		stu.setAttribute("resident",resident);
		String phoneCode = (String)stu.getAttribute( "phoneCode" );
		out.println("Resident: " + resident + "</br>");
		out.println("Phone Code: " + phoneCode + "</br>");
	}
	else if(request.getParameter( "moreDegrees" ) == null){
		String state = request.getParameter( "state" );
		String zipCode = request.getParameter( "zipCode" );
		out.println("State: " + state + "</br>");
		out.println("Zip Code: " + zipCode + "</br>");
	}
	out.println("Phone: " + phone  + "</br>");
	session.setAttribute("student", stu);

	
%>	
	
	
	
	<h2>Choose University</h2>

	<% 
	   	support s = new support();   	
	   	
	   	String path2 = config.getServletContext().getRealPath("universities.txt");
	    Vector universities = s.getUniversities(path2);
	    
	    String location = request.getParameter( "location" );
	    
  		//if degrees is equal to null this is really bad and should almost never happen
  		DegreeContainer degrees = (DegreeContainer)session.getAttribute("DegreeContainer");

	    degree d = degrees.getLastDegree();
	    d.setAttribute("location", location);
	    
	    //add container back into session object
	    session.setAttribute("DegreeContainer", degrees);
    %>
	<h3><%= location %></h3>
	<form method="GET" action="discipline.jsp">
		<p>Manually enter university:</p>
		<input type="text" name="university" /> <input type="submit"
			value="submit" />
	</form>

	<p>Or choose a university from the list.</p>
	<table border="1">
		<tr>
			<td>
			<%for (int i=0; i<universities.size(); i++){
			      Vector tuple = (Vector)universities.get(i);
			      String c = (String)tuple.get(0);
			      
			      if(c.equals(location)){
				      Vector u = (Vector)tuple.get(1);
				      String university = "";
				      
				      
					    int size = u.size();
					    if(size <= 3){
					    	for(int j=0; j<size; j++){
						      	  out.println("<a href=\'discipline.jsp?university=" + u.get(j) + "\'>" + u.get(j) + "</a><br>");
						      }
					    }
					    else{
						    int incr = size/3;
						  	int size1 = 0;
						  	int size2 = size1 + incr;
						  	int size3 = size2 + incr;
						  	int size4 = size;%>
							<table border="1">
							<td>
								<%for(int j=size1; j<size2; j++){
								      	  out.println("<a href=\'discipline.jsp?university=" + u.get(j) + "\'>" + u.get(j) + "</a><br>");
								}%>
							</td>
							<td>
								<%//print all the countries
							    for(int j=size2; j<size3; j++){
							    	out.println("<a href=\'discipline.jsp?university=" + u.get(j) + "\'>" + u.get(j) + "</a><br>");
							    }%>
							</td>
							<td>
								<%//print all the countries
							    for(int j=size3; j<size4; j++){
							    	out.println("<a href=\'discipline.jsp?university=" + u.get(j) + "\'>" + u.get(j) + "</a><br>");
							    }%>
							</td>
							</table>
					     <%}
				      break;
			      }
		    }%>
			</td>
		</tr>
	</table>

</body>
</html>